package com.example.springbootdemo.test;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.MessageManager;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;
import lombok.extern.slf4j.Slf4j;
 
/**
 * @author caozhen
 * @ClassName AliTongYiQianWen
 * @description: 阿里-通义千问
 * @date 2024年01月22日
 * @version: 1.0
 */
@Slf4j
public class AliTongYiQianWen {
 
    public static String simpleMultiModalConversationCall(String question)
            throws ApiException, NoApiKeyException, InputRequiredException {
        Constants.apiKey = "sk-2dabaa795ea3409998ca3dd393d108ef";//这里填写自己申请的APIKEY
        Generation gen = new Generation();
        MessageManager msgManager = new MessageManager(10);
        Message systemMsg =
                Message.builder().role(Role.SYSTEM.getValue()).content("You are a helpful assistant.").build();
        Message userMsg = Message.builder().role(Role.USER.getValue()).content(question).build();
        msgManager.add(systemMsg);
        msgManager.add(userMsg);
        QwenParam param =
                QwenParam.builder().model(Generation.Models.QWEN_TURBO).messages(msgManager.get())
                        .resultFormat(QwenParam.ResultFormat.MESSAGE)
                        .topP(0.8)
                        .enableSearch(true)
                        .build();
        GenerationResult result = gen.call(param);
        String str = result.getOutput().getChoices().get(0).getMessage().getContent();
        log.info("通义千问回答content：{}", str);
        return str;
    }
 
    public static void main(String[] args) throws NoApiKeyException, InputRequiredException {
        System.out.println(simpleMultiModalConversationCall("您好"));
    }
 
}